<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>录入订单</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
	<style>
	
	.product{width:500px; padding:5px;background: #FFF;margin-top:5px;border:1px solid #f1f1f1}
	.product .content{display:flex;position:relative;width: 100%; padding:15px 10px;border-bottom: 1px #e5e5e5 dashed;position:relative;box-sizing:border-box;}
	.product .content:last-child{ border-bottom: 0; }
	.product .content img{ width: 70px; height: 70px;}
	.product .content .detail{display:flex;flex-direction:column;margin-left:7px;flex:1}
	.product .content .detail .t1{height: 30px;line-height: 15px;color: #000;}
	.product .content .detail .t2{height: 23px;line-height: 23px;color: #999;overflow: hidden;font-size:13px;}
	.product .content .detail .t3{display:flex;height:15px;line-height:15px;color: #ff4246;}
	.product .content .detail .x1{ flex:1}
	.product .content .detail .x2{ width:50px;font-size:16px;text-align:right;margin-right:4px}
	.product .content .comment{position:absolute;top:32px;right:5px;border: 1px #ffc702 solid; border-radius:5px;background:#fff; color: #ffc702;  padding: 0 5px; height: 23px; line-height: 23px;}
	#ggnamediv .layui-input,#ggvaldiv .layui-input{ display:inline;height:30px}
	</style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-card layui-col-md12">
				<div class="layui-card-header"><i class="fa fa-cog"></i> 录入订单</div>
				<div class="layui-card-body" pad15>
					<div class="layui-form" lay-filter="">
						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">{:t('会员')}ID：</label>
							<div class="layui-input-inline">
								<input type="text" name="mid" value="" class="layui-input">
							</div>
							<button class="layui-btn layui-btn-primary" style="float:left" onclick="openmax('{:url('member/index')}/isopen/1')">查找{:t('会员')}ID</button>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">选择商品：</label>
							<button class="layui-btn layui-btn-primary" style="float:left" onclick="showChooseProduct()">选择</button>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">已选商品：</label>
							<div class="layui-input-inline">
								<div class="product" id="prolistinfo"></div>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">商品总价：</label>
							<div class="layui-input-inline"><input type="text" name="goodsprice" value="0" class="layui-input" readonly></div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">收货人姓名：</label>
							<div class="layui-input-inline"><input type="text" name="linkman" value="" class="layui-input"></div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">收货人电话：</label>
							<div class="layui-input-inline"><input type="text" name="tel" value="" class="layui-input"></div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">收货地址：</label>
							<div class="layui-input-inline" style="width:500px"><input type="text" name="address" value="" class="layui-input"></div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">配送方式：</label>
							<div class="layui-input-inline"><input type="text" name="freight" value="商家配送" class="layui-input"></div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">配送费：</label>
							<div class="layui-input-inline"><input type="text" name="freightprice" value="0" class="layui-input" onblur="showproinfo()"></div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">支付方式：</label>
							<div class="layui-input-inline"><input type="text" name="paytype" value="线下支付" class="layui-input"></div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label" style="width:100px">总金额：</label>
							<div class="layui-input-inline"><input type="text" name="totalprice" value="" class="layui-input"></div>
						</div>

						<div class="layui-form-item">
							<div class="layui-input-block" style="margin-left:130px;">
								<button class="layui-btn" lay-submit lay-filter="formsubmit">提 交</button>
							</div>
						</div>
					</div>
				</div>
			</div>
    </div>
  </div>
	{include file="public/js"/}
	<script>

	layui.form.on('submit(formsubmit)', function(obj){
		var field = obj.field
		console.log(field);
		field.prodata = buydatastr
		if(field.mid==''){
			layer.msg('请输入{:t('会员')}ID');return;
		}
		if(field.buydatastr==''){
			layer.msg('请选择商品');return;
		}
		var index = layer.load();
		$.post("{:url('save')}",field,function(res){
			layer.close(index);
			dialog(res);
		})
	})
	var chooseProductLayer;
	var levelid = 0;
	function showChooseProduct(){
		chooseProductLayer = layer.open({type:2,title:'选择商品',content:"{:url('shop_product/chooseproduct')}/hidebid/1",area:['900px','600px'],shadeClose:true});
		var mid = $("input[name='mid']").val();
		$.post("{:url('getUser')}", {mid:mid},function(resm){
			// console.log(res);
			if(resm.levelid) {
				levelid = resm.levelid;
			}
		})
	}
	function choosepro(data){
		showgg(data.product,data.guigedata,data.gglist);
	}
	var buydata = [];
	var buydatastr = '';
	function showgg(product,specs,gglist){
			var len = specs.length;
			var newlen = 1; 
			var h = new Array(len); 
			var rowspans = new Array(len);
			var mid = $("input[name='mid']").val();

			var html = '<div style="margin:10px"><table id="ggvaldiv" class="layui-table"><thead><tr>';
			for(var i=0;i<len;i++){
				html+="<th>" + specs[i].title + "</th>";
				var itemlen = specs[i].items.length;
				if(itemlen<=0) { itemlen = 1 };
				newlen*=itemlen;
				h[i] = new Array(newlen);
				for(var j=0;j<newlen;j++){
					h[i][j] = new Array();
				}
				var l = specs[i].items.length;
				rowspans[i] = 1;
				for(j=i+1;j<len;j++){
					rowspans[i]*= specs[j].items.length;
				}
			}
			html += '<th>库存</th>';
			html += '<th>市场价</th>';
			html += '<th>销售价</th>';
			html += '<th>购买数量</th>';
			html += '</tr></thead>';
			
			for(var m=0;m<len;m++){
				var k = 0,kid = 0,n=0;
				for(var j=0;j<newlen;j++){
					var rowspan = rowspans[m]; 
					if( j % rowspan==0){
						h[m][j]={ k:specs[m].items[kid].k,title: specs[m].items[kid].title, html: "<td rowspan='" +rowspan + "'>"+ specs[m].items[kid].title+"</td>\r\n",id: specs[m].items[kid].id};
					}else{
						h[m][j]={ k:specs[m].items[kid].k,title:specs[m].items[kid].title, html: "",id: specs[m].items[kid].id};	
					}
					n++;
					if(n==rowspan){
						kid++; if(kid>specs[m].items.length-1) { kid=0; }
						n=0;
					}
				}
			}
			var hh = "";
			for(var i=0;i<newlen;i++){
				hh+="<tr>";
				var ks = [];
				var titles = [];
				for(var j=0;j<len;j++){
					hh+=h[j][i].html; 
					ks.push( h[j][i].k);
					titles.push( h[j][i].title);
				}
				ks =ks.join(',');
				titles =titles.join(',');
				if(typeof(gglist[ks])!='undefined'){
					var val = gglist[ks];
				}else{
					var val = { procode:'',market_price:'',cost_price:'',sell_price:'',weight:'',stock:'1000',pic:''};
				}
				if(levelid && product.lvprice) {
					var lvprice = JSON.parse(val['lvprice_data']);
					if(lvprice[levelid] >= 0)
					val['sell_price'] = lvprice[levelid];
				}
				hh += '<td>'+val.stock+'</td>';
				hh += '<td>'+val.market_price+'</td>';
				hh += '<td>'+val.sell_price+'</td>';
				hh += '<td>';
				hh += ' <input name="buynum" ggid="'+val['id']+'" sellprice="'+val['sell_price']+'" ggname="'+val['name']+'" lvprice=\''+val['lvprice_data']+'\' type="num" style="width:80px" value="0" class="layui-input"/>';
				hh += '</td>';
				hh += "</tr>";
			}
			html+=hh;
			html+='</table></div>';
			layer.open({type:1,title:product.name,content:html,area:['900px','600px'],shadeClose:true,btn:['确定','取消'],
				yes:function(index){
					layer.close(index);
					$('input[name=buynum]').each(function(){
						var ggid = $(this).attr('ggid');
						var ggname = $(this).attr('ggname');
						var sell_price = $(this).attr('sellprice');
						var buynum = $(this).val();
						if(buynum > 0){
							var thisdata = {'name':product.name,'pic':product.pic,'id':product.id,'ggname':ggname,'ggid':ggid,'sell_price':sell_price,'buynum':buynum}
							buydata.push(thisdata);
						}
					});
					showproinfo()
				}
			});
		}
		function showproinfo(){
			var html = '';
			var totalgoodsprice = 0;
			buydatastr = '';
			buydatastrArr = [];
			for(var i in buydata){
				var m = buydata[i]
				html += '<div class="content">';
				html += '	<div>';
				html += '		<img src="'+m.pic+'"/>';
				html += '	</div>';
				html += '	<div class="detail">';
				html += '		<span class="t1">'+m.name+'</span>';
				html += '		<span class="t2">'+m.ggname+'</span>';
				html += '		<div class="t3"><span class="x1 flex1">￥'+m.sell_price+'</span><span class="x2">×'+m.buynum+'</span></div>';
				html += '	</div>';
				html += '</div>';
				totalgoodsprice += m.sell_price * m.buynum
				buydatastrArr.push(m.id + ',' + m.ggid + ',' + m.buynum);
			}
			buydatastr = buydatastrArr.join('-');
			var freightprice = parseFloat($('input[name=freightprice]').val());
			var totalprice = totalgoodsprice + freightprice
			totalgoodsprice = totalgoodsprice.toFixed(2);
			totalprice = totalprice.toFixed(2);
			$('#prolistinfo').html(html);
			$('input[name=goodsprice]').val(totalgoodsprice);
			$('input[name=totalprice]').val(totalprice);
		}

		function plset(name){
			if(name =='procode'){
				var procode = $("input[name$='["+name+"]']").eq(0).val();
				if(procode == '') procode = randomString(6);
				$("input[name$='["+name+"]']").each(function(i,v){
					if(i>0) $(this).val(procode+ '-'+i);
				})
			}else{
				$("input[name$='["+name+"]']").val($("input[name$='["+name+"]']").eq(0).val());
			}
		}
  </script>
	{include file="public/copyright"/}
</body>
</html>